<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\ImageRequest;
use App\Models\Image;
use App\Transformers\ImageTransformer;
use Utility\library\images\ImageUpload;

class ImagesController extends Controller
{
    //图片上传接口
    public function store(ImageRequest $request,ImageUpload $imageUpload,Image $image)
    {
        $user=\Auth::guard('api')->user();
        //如果类型为头像,则宽度为400,否则为1024
        $size=$request->type=='avatar'?400:1024;
        $result=$imageUpload->upload($request->image,'avatar',$user->id,$size);
        $image->path=$result['path'];
        $image->type=$request->type;
        $image->user_id=$user->id;
        $image->save();
        return $this->response->item($image,new ImageTransformer())->setStatusCode(201);
    }



}
